Diandiankong = {};


Blockly.Blocks['condtioner'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
      "message0": Blockly.Msg.DDK_CONDITIONER,
      "args0": [
        {
          "type": "input_value",
          "name": "temperature",
          "check": "Number"
        }
      ],
       "prevStatement": "Action",
       "nextStatement": "Action",
      "inputsInline": true,
      "colour": Blockly.Blocks.math.HUE,
      "helpUrl": Blockly.Msg.MATH_ARITHMETIC_HELPURL
    });
    this.setPreviousStatement(!0);
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};
Blockly.Blocks['open_door'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
      "message0": Blockly.Msg.DDK_OPENDOOR,
      
       "prevStatement": "Action",
       "nextStatement": "Action",
      "inputsInline": true,
      "colour": Blockly.Blocks.math.HUE,
      "helpUrl": Blockly.Msg.MATH_ARITHMETIC_HELPURL
    });
    this.setPreviousStatement(!0);
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};
Blockly.Blocks['close_door'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
      "message0": Blockly.Msg.DDK_CLOSEDOOR,
     
       "prevStatement": "Action",
       "nextStatement": "Action",
      "inputsInline": true,
      "colour": Blockly.Blocks.math.HUE,
      "helpUrl": Blockly.Msg.MATH_ARITHMETIC_HELPURL
    });
    this.setPreviousStatement(!0);
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};

Blockly.Blocks['temperature'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
	  "type": "temperature",
	  "message0": Blockly.Msg.DDK_TEMPERATURE ,
	   "output": "Number",
	  "colour": 120
	});
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};

Blockly.Blocks['face_match'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
	  "type": "face_match",
	  "message0": Blockly.Msg.DDK_FACEMATCH ,
	   "output": "Boolean",
	  "colour": 120
	});
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};
Blockly.Blocks['win1'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
	  "type": "win1",
	  "message0": Blockly.Msg.DDK_WIN1 ,
	   "output": "Boolean",
	  "colour": 120
	});
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};
Blockly.Blocks['win2'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
	  "type": "win1",
	  "message0": Blockly.Msg.DDK_WIN2 ,
	   "output": "Boolean",
	  "colour": 120
	});
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};
Blockly.Blocks['win3'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
	  "type": "win1",
	  "message0": Blockly.Msg.DDK_WIN3 ,
	   "output": "Boolean",
	  "colour": 120
	});
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};

Blockly.Blocks['state_save'] = {
  // y variable setter.
  init: function() {
    this.jsonInit({
      "message0": Blockly.Msg.DDK_STATE_SAVE,
      "args0": [
        {
          "type": "input_value",
          "name": "win1",
          "check": "Boolean"
        },
		{
          "type": "input_value",
          "name": "win2",
          "check": "Boolean"
        },
        {
          "type": "input_value",
          "name": "win3",
          "check": "Boolean"
        }
      ],
       "prevStatement": "Action",
       "nextStatement": "Action",
      "inputsInline": false,
      "colour": Blockly.Blocks.math.HUE,
      "helpUrl": Blockly.Msg.MATH_ARITHMETIC_HELPURL
    });
    this.setPreviousStatement(!0);
    // Assign 'this' to a variable for use in the tooltip closure below.
   
  }
};


var workspace = Blockly.inject('blocklyDiv',
        {media: './media/',
         toolbox: document.getElementById('toolbox')});  
Diandiankong.workspace = workspace;

 function showCode() {
          // Generate JavaScript code and display it.
          Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
          var xml = Blockly.Xml.workspaceToDom(Diandiankong.workspace);
          var data = Blockly.Xml.domToText(xml);
          alert(data);

  }



   
      function onchange()
      {
          //runCode();
      }
	function upload() {
          var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
          var data = Blockly.Xml.domToText(xml);
         jQuery.ajax({
			    type: 'POST',
			    url: '/blockly',
			    data: {  xml: data},
			    success: function(response){
			      console.log('ok');
			      
			    },
			    error: function(e){
			      var response = JSON.parse(e.responseText);
			      
			    },
			    dataType: 'json'
			  });
      };

   function load_it() {
          var loadInput = document.getElementById('load');
          loadInput.addEventListener('change', load, false);

          loadInput.click();
      };
      function load() {
          var event = arguments[0]||window.event;
          var files = event.target.files;
          // Only allow uploading one file.
          if (files.length != 1) {
              return;
          }

          // FileReader
          var reader = new FileReader();
          reader.onloadend = function(event) {
              var target = event.target;
              // 2 == FileReader.DONE
              if (target.readyState == 2) {
                  try {
                      var xml = Blockly.Xml.textToDom(target.result);
                  } catch (e) {
                      alert('Error parsing XML:\n' + e);
                      return;
                  }
                  var count = Blockly.mainWorkspace.getAllBlocks().length;
                  if (count && confirm('Replace existing blocks?\n"Cancel" will merge.')) {
                      Blockly.mainWorkspace.clear();
                  }
                  Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
              }
              // Reset value of input after loading because Chrome will not fire
              // a 'change' event if the same file is loaded again.
              document.getElementById('load').value = '';
          };
          reader.readAsText(files[0]);

      };
 Diandiankong.workspace.addChangeListener(onchange);